package cre.sample.test.encoding;

/**
 * @Author penglai
 * @Date 2016/10/11.
 */
public class ISO_8859_1 {
    private byte repl = 63;

    public int encode(char[] ca, int off, int len, byte[] bs) {
        int start = 0;
        int end = Math.min(len, bs.length);
        int var7 = off + end;

        while (off < var7) {
            int var8 = encodeISOArray(ca, off, bs, start, end);
            off += var8;
            start += var8;
            if (var8 != end) {
                char var9 = ca[off++];
                if (Character.isHighSurrogate(var9) && off < var7 && Character.isLowSurrogate(ca[off])) {
                    if (len > bs.length) {
                        ++var7;
                        --len;
                    }

                    ++off;
                }

                bs[start++] = this.repl;
                end = Math.min(var7 - off, bs.length - start);
            }
        }

        return start;

    }

    private static int encodeISOArray(char[] ca, int off, byte[] bs, int start, int end) {
        int i;
        for (i = 0; i < end; ++i) {
            char var6 = ca[off++];
            // 超过一个字节的就break
            if (var6 > 255) {
                break;
            }
            // 一个字节大小的直接转成byte
            bs[start++] = (byte) var6;
        }

        return i;
    }
}
